﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Hosting;
using System.Data.SqlClient;
using System.Data;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //reading cookies
        if (Request.Cookies["userName"] != null)
        {
            tbAccount.Visible = false;
            btLogin.Visible = false;
            lbPassword.Visible = false;
            tbPassword.Visible = false;

            //wellcome user 

            lbAccount.Text = "<p>Wellcom <a>" + Server.HtmlEncode(Request.Cookies["userName"].Value).ToString() + "</a> <br><a href='logout.aspx' style='color:White'>Sign out</a></p>";

            //Thêm các địa điểm vào TreeView------

            string _username = Server.HtmlEncode(Request.Cookies["userName"].Value).ToString();
            List<mapDTO> mapList = new List<mapDTO>();
            mapDAO _mapDAO = new mapDAO();
            mapList = _mapDAO.getListMapByUser(_username);
            if (mapList != null)
            {
                foreach (mapDTO _mapDTO in mapList)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Value = _mapDTO.ID.ToString();
                    newNode.Text = _mapDTO.Name;
                    double vido = _mapDTO.Latitude;
                    double kinhdo = _mapDTO.Longitude;
                    newNode.NavigateUrl = "Default.aspx";
                    newNode.Text = "<div id = '" + newNode.Value.ToString() + "' onmouseover='movemaps(\"" + newNode.Text + "\", " + newNode.Value + ", " + vido + ", " + kinhdo + ")'>" + newNode.Text + "</div>";
                    newNode.NavigateUrl = "Default.aspx";

                    if (_mapDTO.Type == 1)
                        tvDanhSachDD.Nodes[0].ChildNodes[0].ChildNodes.Add(newNode);
                    if (_mapDTO.Type == 2)
                        tvDanhSachDD.Nodes[0].ChildNodes[1].ChildNodes.Add(newNode);
                    if (_mapDTO.Type == 3)
                        tvDanhSachDD.Nodes[0].ChildNodes[2].ChildNodes.Add(newNode);
                }
            }
        }
    }

    protected void btLogin_Click(object sender, EventArgs e)
    {
        string _account = tbAccount.Text.Trim();
        string _password = tbPassword.Text.Trim();
        accountDAO _accountDAO = new accountDAO();
        accountDTO Account = _accountDAO.getAccount(_account, _password);
        if (Account != null)
        {
            tbAccount.Visible = false;
            btLogin.Visible = false;
            lbPassword.Visible = false;
            tbPassword.Visible = false;

            Response.Cookies["userName"].Value = tbAccount.Text;
            Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(20);

            HttpCookie aCookie = new HttpCookie("lastVisit");
            aCookie.Value = DateTime.Now.ToString();
            aCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(aCookie);

            lbAccount.Text = "<p>Wellcom  <a>" + tbAccount.Text + "</a><br><a href='logout.aspx' style='color:White'>Sign out</a></p>";
            Response.Redirect("Default.aspx");
        }
        else
        {
            tbAccount.Text = " Invalid ID or pass";
        }
    }
}
